<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 1、导入mapbox的依赖 -->
    <script src='https://api.mapbox.com/mapbox-gl-js/v2.15.0/mapbox-gl.js'></script>
    <link href='https://api.mapbox.com/mapbox-gl-js/v2.15.0/mapbox-gl.css' rel='stylesheet' />
    <style>
        * {
            margin: 0;
            padding: 0
        }

        #map {
            width: 100vw;
            height: 100vh;
        }
    </style>
</head>

<body>
    <!-- 2、设置地图容器的挂载点 -->
    <div id="map">

    </div>
    <script>
        /* token */
        mapboxgl.accessToken = 'pk.eyJ1IjoiZXJpYzA5MTciLCJhIjoiY2x3cTI2Zzg0MWY2dDJqcWVpdDI1MHNpcCJ9.k9gLW7ZgLyISNdYHIEEGdQ'
        const map = new mapboxgl.Map({
            /* target-ol */
            container: 'map', // container ID
            /* style-layers */
            style: 'mapbox://styles/mapbox/streets-v12', // style URL
            center: [114.30, 30.50], // starting position [lng, lat]
            zoom: 1, // starting zoom
        });
        let isUserInterface = false;
        map.on("style.load", () => {
            map.setFog({})
            rotateMap();
        })
        map.on("moveend", () => {
            console.log("end")
            rotateMap();
        })
        map.on("click", () => {
            isUserInterface = !isUserInterface;
            if (isUserInterface) {
                map.stop();
            } else {
                rotateMap();
            }
        })
        function rotateMap() {
            /* 只有用户没有触碰才旋转 && zoom>4*/
            const zoom = map.getZoom();
            if (!isUserInterface && zoom < 4) {
                let center = map.getCenter();
                center.lng += 5;
                /* easeTo可以实现动画 */
                map.easeTo({
                    center,
                    duration: 1000,
                    easing: n => n
                })
            }

        }
        /*
        1、地球自动旋转
        2、执行点击事件控制地球的旋转/暂停
         */
    </script>
</body>

</html>